//
//  DownloadData.m
//  iStock Global
//
//  Created by Andy on 2010/10/13.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "DownloadData.h"


@implementation DownloadData
//***{ 網路下載資料範本
@synthesize receivedData;
@synthesize connection;
@synthesize loadingData;
@synthesize DownloadData;
@synthesize delgete;

//***}
-(void)StartDownload:(NSString *)urls identifier:(NSString *)Aidentifier{
	 
	identifier=Aidentifier;
	 
	NSURL *url = [NSURL URLWithString:urls];
	 
	NSURLRequest *theRequest=[NSURLRequest requestWithURL:url
										   cachePolicy:NSURLRequestUseProtocolCachePolicy
										   timeoutInterval:60.0];
	
	// create the connection with the request
	// and start loading the data
	self.connection = [NSURLConnection connectionWithRequest:theRequest delegate:self];
	if (self.connection) {
		self.receivedData = [NSMutableData data];
	} 
	else {
		self.loadingData = NO;
	}    	
	
	//[urls  release];
	
}
-(void)StopDownload{
	if (self.loadingData) {
        [self.connection cancel];
        self.loadingData = NO;
        self.receivedData = nil;
        self.connection = nil;
		
    }
}


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    // append the new data to the receivedData
	//lab.text=@"downloda...";
	[self.receivedData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    // this method is called when the server has determined that it
    // has enough information to create the NSURLResponse
    // it can be called multiple times, for example in the case of a
    // redirect, so each time we reset the data.
    [self.receivedData setLength:0];
}
-(void)cancelDownload{
    if (self.loadingData) {
        [self.connection cancel];
        self.loadingData = NO;
        self.receivedData = nil;
        self.connection = nil;
		
    }
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    self.loadingData = NO;
    self.receivedData = nil;
    self.connection = nil;
	self.connection = nil;
	NSLog(@"self.delgete=%@",self.delgete);
	[self.delgete Download_didFailWithError:error Oidentifier:identifier];

	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;	
	 
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
	// NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
	
	//self.connection = nil;    	
	
    //NSASCIIStringEncoding
    
    //標記:下載資料的編碼分兩種 因為下載歐洲股票會產生歐元符號 所以將原本的NSUTF8StringEncoding 改為NSASCIIStringEncoding    
    
    //NSString *csv = [ [NSString alloc]  initWithData:self.receivedData encoding:NSUTF8StringEncoding];
    
    
    
    
    
    
    NSString *csv = [ [NSString alloc]  initWithData:self.receivedData encoding:NSASCIIStringEncoding];    
    
 
    [self.delgete Download_Finish:csv Oidentifier:identifier];
   
    [csv autorelease];
    
   
    //最後清除資料
	// [self.receivedData release];
	//self.receivedData = nil;
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;	
	self.loadingData = NO;
    
    
    
	//[csv  release];    
    
     //[pool release];
	
}


-(void)dealloc {
	[receivedData release];
	receivedData=nil;
	
	[connection release];
    connection=nil;
	
	[identifier release];
	identifier=nil;
	
	 
	
	[super dealloc];
}


@end
